package com.qsl.ssyx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qsl.ssyx.enums.user.Leader;
import com.qsl.ssyx.enums.user.User;
import com.qsl.ssyx.enums.user.UserDelivery;
import com.qsl.ssyx.user.mapper.UserMapper;
import com.qsl.ssyx.user.service.LeaderService;
import com.qsl.ssyx.user.service.UserDeliveryService;
import com.qsl.ssyx.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qsl.ssyx.user.utils.WXPublicAccount;
import com.qsl.ssyx.vo.user.LeaderAddressVo;
import com.qsl.ssyx.vo.user.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 青衫泪
 * @since 2023-08-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserDeliveryService userDeliveryService;

    @Autowired
    private LeaderService leaderService;

    // 根据openid查询用户信息
    @Override
    public User getUserByOpenId(String openid) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getOpenId, openid);
        return baseMapper.selectOne(lqw);
    }

    // 根据userId查询提货点和团长信息
    @Override
    public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
        // 根据userId查询用户默认的团长id
        LambdaQueryWrapper<UserDelivery> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserDelivery::getUserId, userId);
        lqw.eq(UserDelivery::getIsDefault, 1); // 团长id
        UserDelivery userDelivery = userDeliveryService.getOne(lqw);

        if (userDelivery == null) {
            return null;
        }

        // 根据团长id，查询leader团长表信息
        Leader leader = leaderService.getById(userDelivery.getLeaderId());

        // 封装返回数据
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
        BeanUtils.copyProperties(leader, leaderAddressVo);
        leaderAddressVo.setUserId(userId);
        leaderAddressVo.setLeaderId(leader.getId());
        leaderAddressVo.setLeaderName(leader.getName());
        leaderAddressVo.setLeaderPhone(leader.getPhone());
        leaderAddressVo.setWareId(userDelivery.getWareId());
        leaderAddressVo.setStorePath(leader.getStorePath());
        return leaderAddressVo;
    }

    // 根据userId获取当前登录用户信息
    @Override
    public UserLoginVo getUserLoginVo(Long userId) {
        User user = baseMapper.selectById(userId);
        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtils.copyProperties(user, userLoginVo); // user对象数据 复制到 userLoginVo
        userLoginVo.setUserId(userId);

        // 根据userId查询是否唯一
        UserDelivery userDelivery = userDeliveryService.getUserDeliveryOneByUserId(userId);

        if (userDelivery != null) {
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
            userLoginVo.setWareId(userDelivery.getWareId());
        } else {
            userLoginVo.setLeaderId(1L); // 当前登录用户团长id
            userLoginVo.setWareId(1L); // 仓库id
        }

        return userLoginVo;
    }
}
